import 'package:rjpb/utils/http_util.dart';
import 'package:rjpb/config/config.dart';

class FireAlarmService {
  static Future<Map<String, dynamic>> fetchInspectionData({
    required int pageNum,
    required int pageSize,
  }) async {
    final url = '$baseUrl/wgq/scheme/list';
    try {
      final response = await HttpUtil().get(
        url,
        data: {
          "pageNum": pageNum,
          "pageSize": pageSize,
        },
      );
      return response as Map<String, dynamic>;
    } catch (e) {
      print("请求失败: $e");
      return {
        "code": 500,
        "msg": "请求失败",
        "rows": [],
      };
    }
  }

  /// 获取菜单数据
  static Future<List<Map<String, dynamic>>> fetchMenuData() async {
    const url = '$baseUrl/wgq/location/cascadeData';
    try {
      // final response = await HttpUtil().get(url);
      final response = await HttpUtil().get(
        url,
        data: {
          "rank": 4,
        },
      );
      if (response['code'] == 200 && response['data'] is List) {
        // 将返回的 data 转换为 List<Map<String, dynamic>>
        print("菜单数据: ${response['data']}");
        return List<Map<String, dynamic>>.from(response['data']);
      } else {
        print("菜单数据格式错误: ${response['msg']}");
        return [];
      }
    } catch (e) {
      print("请求失败: $e");
      return [];
    }
  }

  //根据选中菜单的Id请求对应的svg图
  static Future<String?> fetchSvgData(String menuId) async {
    final url = '$baseUrl/screen/location/$menuId';
    try {
      final response = await HttpUtil().get(url);
      if (response['code'] == 200 && response['data'] != null) {
        print("SVG数据: ${response['data']}");
        return response['data']['spare1'] as String?;
      } else {
        print("获取SVG数据失败: ${response['msg']}");
        return null;
      }
    } catch (e) {
      print("请求失败: $e");
      return null;
    }
  }

  //获取选中菜单对应的svg图上的点位
  static Future<List<Map<String, dynamic>>> fetchPointData(
      String locationId) async {
    print("重要: $locationId");
    final url = '$baseUrl/screen/loc-dot/list';
    try {
      final response = await HttpUtil().get(
        url,
        data: {
          "locId": locationId,
          "deviceID": 0,
        },
      );
      print("获取点位数据: $response");
      if (response['code'] == 200 && response['data'] is List) {
        print("点位数据: ${response['data']}");
        return List<Map<String, dynamic>>.from(response['data']);
      } else {
        print("获取点位数据失败: ${response['msg']}");
        return [];
      }
    } catch (e) {
      print("请求失败: $e");
      return [];
    }
  }

  // 请求点位总数数
  // static Future<int> fetchAbnormalPortCount() async {
  //   final url = '$baseUrl/wgq/abnormalPortCount';
  //   try {
  //     final response = await HttpUtil().get(url);
  //     if (response['code'] == 200) {
  //       return response['data']['count'] ?? 0; // 返回异常数
  //     } else {
  //       print('获取串口异常数失败: ${response['msg']}');
  //       return 0;
  //     }
  //   } catch (e) {
  //     print('请求失败: $e');
  //     return 0;
  //   }
  // }

  // 请求点位数量
  static Future<Map<String, dynamic>> fetchDotCount() async {
    const url = '$baseUrl/wgq/dot/getAllLocDot';
    try {
      final response = await HttpUtil().get(url);
      print("获取点位数量数据: $response");
      return response as Map<String, dynamic>;
    } catch (e) {
      print("请求点位数量失败: $e");
      return {
        "code": 500,
        "msg": "请求失败",
      };
    }
  }

  // 请求标签页数据
  static Future<Map<String, dynamic>> fetchAlarmList({
    required int pageNum,
    required int pageSize,
    required String infocode,
    required bool today,
    // required String location,
  }) async {
    final url = '$baseUrl/rp4/alarm/list';
    final requestData = {
      "pageNum": pageNum,
      "pageSize": pageSize,
      "infoCode": infocode,
      "today": today,
      // "location": location,
    };

    print("请求参数: $requestData"); // 打印请求参数
    try {
      final response = await HttpUtil().get(
        url,
        data: requestData,
      );
      print("请求数据数据: $response");
      return response as Map<String, dynamic>;
    } catch (e) {
      print("请求失败: $e");
      return {
        "code": 500,
        "msg": "请求失败",
        "rows": [],
      };
    }
  }

  //根据报警id请求楼层id
  static Future<Map<String, dynamic>> fetchFloorData(int cardId) async {
    final url = '$baseUrl/rp4/alarm/getFloor/$cardId';
    print("请求楼层数据的url: $url");
    try {
      final response = await HttpUtil().get(
        url,
      );
      print("请求楼层数据: $response");
      return response as Map<String, dynamic>;
    } catch (e) {
      print("请求楼层数据失败: $e");
      return {
        "code": 500,
        "msg": "请求失败",
      };
    }
  }
}
